<template>
    <div class="white-box">
        <title-more title="全局筛选">
            <template #sub>
                <el-popover placement="right-start" title="" :width="300" trigger="hover">
                    <template #reference>
                        <el-icon color="#C1C1C1" :size="18"><QuestionFilled /></el-icon>
                    </template>
                    <template #default>
                        <p class="p-popover">默认筛选当天</p>
                        <p class="p-popover">统计时间：从当天选择日期往前推一个月</p>
                    </template>
                </el-popover>
            </template>
        </title-more>
        <div class="task-one" style="margin-bottom: 0">
            <div class="task-one__item">
                <span class="task-one__label">选择时间：</span>
                <el-date-picker v-model="area_time" type="date" value-format="YYYY-MM-DD" :disabled-date="disabledDate" placeholder="请选择时间" />
            </div>
            <div class="task-one__btn">
                <el-button type="primary" :loading="search_btn" :icon="Search" @click="handleSearch">查询</el-button>
            </div>
        </div>
    </div>
    <div class="white-box">
        <title-more title="数据概览">
            <template #sub>
                <el-popover placement="right-start" title="" :width="700" trigger="hover">
                    <template #reference>
                        <el-icon color="#C1C1C1" :size="18"><QuestionFilled /></el-icon>
                    </template>
                    <template #default>
                        <p class="p-popover">注：实时数据仅供参考，有60s左右延迟，准确数据以天级数据为准</p>
                        <p class="p-popover">浏览量：今日凌晨至更新时间，店铺页面被浏览的次数。每打开一个页面或每刷新一次页面都记录1次</p>
                        <p class="p-popover">访客量：今日凌晨至更新时间，进入店铺的去重访客量</p>
                        <p class="p-popover">累计用户：统计周期内所有用户，不会随着筛选条件变化</p>
                        <p class="p-popover">新增用户：今日凌晨至更新时间，店铺新增的用户</p>
                    </template>
                </el-popover>
            </template>
        </title-more>
        <div class="data-prev flexs">
            <div class="data-prev__item">
                <div class="data-prev__tip">浏览量</div>
                <p class="data-prev__nums">{{ objs.views || "-" }}</p>
            </div>
            <div class="data-prev__item">
                <div class="data-prev__tip">访客量</div>
                <p class="data-prev__nums">{{ objs.visitors || "-" }}</p>
            </div>
            <div class="data-prev__item">
                <div class="data-prev__tip">累计用户</div>
                <p class="data-prev__nums">{{ objs.total_user || "-" }}</p>
            </div>
            <div class="data-prev__item">
                <div class="data-prev__tip">新增用户</div>
                <p class="data-prev__nums">{{ objs.new_user || "-" }}</p>
            </div>
        </div>
    </div>
    <!-- 近30天PV/UV情况 -->
    <div class="white-box">
        <title-more title="近30天PV/UV情况"></title-more>
        <div class="echars-one__cont">
            <div id="echart-pvuv" class="echart-view"></div>
        </div>
    </div>
    <!-- 近30天新增/累计用户 -->
    <div class="white-box">
        <title-more title="近30天新增/累计用户"></title-more>
        <div class="echars-one__cont">
            <div id="echart-user" class="echart-view"></div>
        </div>
    </div>
    <go-back />
</template>

<script setup>
import { getCurrentInstance, ref, onMounted, onBeforeUnmount } from "vue"
import { Search, QuestionFilled } from "@element-plus/icons-vue"
import TitleMore from "@components/title-more.vue"
import GoBack from "@components/GoBack.vue"

// echarts
import EchartsLine from "@common/echarts-line"

const { proxy } = getCurrentInstance()
console.log();
onMounted(() => {
    getDataInfo()
    window.onresize = echartResize
})

onBeforeUnmount(() => {
    echartsDispose()
    window.onresize = null
})

// 筛选
const area_time = ref("")
const search_btn = ref(false)
const handleSearch = () => {
    search_btn.value = true
    echartsDispose()
    getDataInfo()
}

// 概况
const objs = ref({
    views: 0,
    visitors: 0,
    total_user: 0,
    new_user: 0,
})
function getDataInfo() {
    proxy.$axios
        .get("flow/overview/tongji", {
            params: {
                day: area_time.value,
            },
        })
        .then((res) => {
            if (res.data.code == 0) {
                let result = res.data.data
                objs.value.views = result.pv_count
                objs.value.visitors = result.uv_count
                objs.value.total_user = result.total_user_count
                objs.value.new_user = result.month_user_count
                pvAndUv()
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => {})
}

/**
 * 图表
 * @param {Object} chart_pv UV/PV实例
 * @param {Object} chart_user 新增/累计用户
 */
let chart_pv = null
let chart_user = null
function echartResize() {
    chart_pv.resize()
    chart_user.resize()
}
function echartsDispose() {
    chart_pv != null && chart_pv.dispose()
    chart_user != null && chart_user.dispose()
    chart_pv = null
    chart_user = null
}

// PV/UV
function pvAndUv() {
    chart_pv = proxy.$echarts.init(document.getElementById("echart-pvuv"))
    let uvs = [],
        pvs = [],
        xData = []
    proxy.$axios
        .get("flow/overview/view",{
            params: {
                day: area_time.value,
            },
        })
        .then((res) => {
            if (res.data.code == 0) {
                let result = res.data.data.view_items
                result.map((item) => {
                    pvs.push(item.pv_count)
                    uvs.push(item.uv_count)
                    xData.push(item.date)
                })
                let series = [
                    {
                        data: uvs,
                        type: "line",
                        name: "UV",
                        smooth: true, // 是否平滑曲线显示。
                        showSymbol: false, // 是否显示 symbol, 如果 false 则只有在 tooltip hover 的时候显示。
                        areaStyle: {
                            opacity: 1,
                            color: new proxy.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                {
                                    offset: 0,
                                    color: "rgba(64, 158, 255,0.8)",
                                },
                                {
                                    offset: 1,
                                    color: "rgba(255, 255, 255, 0.3)",
                                },
                            ]),
                        },
                    },
                    {
                        data: pvs,
                        type: "line",
                        name: "PV",
                        smooth: true,
                        showSymbol: false,
                        areaStyle: {
                            opacity: 1,
                            color: new proxy.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                {
                                    offset: 0,
                                    color: "rgba(255, 155, 44,0.8)",
                                },
                                {
                                    offset: 1,
                                    color: "rgba(255, 255, 255, 0.3)",
                                },
                            ]),
                        },
                    },
                ]
                let legend = ["UV", "PV"]
                let option = EchartsLine(xData, legend, series)
                chart_pv.setOption(option)

                userEchart()
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => {})
}

// 新增/累计用户
function userEchart() {
    chart_user = proxy.$echarts.init(document.getElementById("echart-user"))
    let nums = [],
        totalNums = [],
        xData = []
    proxy.$axios
        .get("flow/overview/user",{
            params: {
                day: area_time.value,
            },
        })
        .then((res) => {
            search_btn.value = false
            if (res.data.code == 0) {
                let result = res.data.data.user_items
                result.map((item) => {
                    nums.push(item.user_num)
                    totalNums.push(item.total_num)
                    xData.push(item.date)
                })
                let series = [
                    {
                        data: nums,
                        type: "line",
                        name: "新增用户",
                        smooth: true,
                        showSymbol: false,
                    },
                    {
                        data: totalNums,
                        type: "line",
                        name: "累计用户",
                        smooth: true,
                        showSymbol: false,
                    },
                ]
                let legend = ["新增用户", "累计用户"]
                let option = EchartsLine(xData, legend, series)
                chart_user.setOption(option)
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => {
            search_btn.value = false
        })
}

const disabledDate = (time) => {
    return time.getTime() > Date.now()
}
</script>
<script>
export default {
    name: "FlowOverview",
}
</script>

<style lang="less" scoped>
.echart-view {
    width: 100%;
    height: 340px;
}
.p-popover {
    font-size: @font-size-md;
    color: #595961;
    margin: @margin-xs 0px;
}
.data-prev {
    .data-prev__item {
        width: 24%;
        padding: @padding-mdl;
        overflow: hidden;
        border-radius: 10px;
        .data-prev__tip {
            margin-bottom: @margin-md;
            color: @gray-6;
        }
        .data-prev__nums {
            font-size: 26px;
            color: #111;
        }
        &:hover {
            background-color: #f4f8ff;
        }
    }
}
</style>
